﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using System.Data;

namespace Laugh.Data
{
    public class UnitOfWork : IUnitOfWork
    {
        //private ISession m_Session;
        //protected Dictionary<IEntity, IRepository<IEntity>> m_AddedEntities;
        //protected Dictionary<IEntity, IRepository<IEntity>> m_UpdatedEntities;
        //protected Dictionary<IEntity, IRepository<IEntity>> m_DeletedEntities;

        //internal UnitOfWork()
        //{
        //    //this.m_Session = DatabaseManager.CurrentFactory.OpenSession();
        //    this.m_AddedEntities = new Dictionary<IEntity, IRepository<IEntity>>();
        //    this.m_UpdatedEntities = new Dictionary<IEntity, IRepository<IEntity>>();
        //    this.m_DeletedEntities = new Dictionary<IEntity, IRepository<IEntity>>();
        //}

        //public void RegisterUpdate(IEntity entity, IRepositoryBase repository)
        //{
        //    this.m_UpdatedEntities.Add(entity, repository);
        //}


        //public void RegisterAdded(IEntity entity, IRepositoryBase repository)
        //{
        //    this.m_AddedEntities.Add(entity, repository);
        //}

        //public void RegisterDeleted(IEntity entity, IRepositoryBase repository)
        //{
        //    this.m_DeletedEntities.Add(entity, repository);
        //}

        //public IDictionary<IEntity, long> Commit()
        //{
        //    return Commit(IsolationLevel.Unspecified);
        //}

        //public IDictionary<IEntity, long> Commit(IsolationLevel isolationLevel)
        //{
        //    var dic = new Dictionary<IEntity, long>();
        //    using (var transaction = this.m_Session.BeginTransaction(isolationLevel))
        //    {
        //        this.m_AddedEntities.Keys.ToList().ForEach(entity =>
        //        {
        //            var id = this.m_AddedEntities[entity].Add(entity);
        //            dic.Add(entity, id);
        //        });
        //        this.m_UpdatedEntities.Keys.ToList().ForEach(entity =>
        //        {
        //            this.m_UpdatedEntities[entity].Update(entity);
        //        });
        //        this.m_DeletedEntities.Keys.ToList().ForEach(entity =>
        //        {
        //            this.m_DeletedEntities[entity].Delete(entity);
        //        });
        //        transaction.Commit();
        //    }
        //    return dic;
        //}

        //public IDisposable SessionOrDBContext
        //{
        //    get { return this.m_Session; }
        //    set { }
        //}

        //public void Dispose()
        //{
        //    if (this.m_Session.IsOpen)
        //        this.m_Session.Close();
        //}
    }
}
